//
//  MainViews.m
//  MobileZIPCodes
//
//  Created by Andreas Schwelling on 23.11.07.
//	http://www.andi.de/zipcodes/
//
//  Released under the GNU General Public License.
//  See LICENSE for details.
//
//  Use at your own risk.
//
#import "MainView.h"


@implementation MainView

- (id)initWithFrame:(struct CGRect)rect {
	
	
	_dm = [DataManager alloc];
	
	NSDate *now = [[NSDate date] retain];
	
	NSString *lastrunplist = [_dm getPListParam:@"lastupdate"];
	
	//NSLog(@"lastrunplist : %@",lastrunplist);
	
	NSDate *lastStart = [[NSDate dateWithString:lastrunplist] retain];
	
	//NSLog(@"lastStart : %@",lastStart);
	
	//NSLog(@"timeinterval: %d",[now timeIntervalSinceDate:lastStart]);
	
	if ([now timeIntervalSinceDate:lastStart]>1800) {
		_performUpdate = YES;
		//NSLog(@"update of remote specs necessary");
		
		[_dm setPListParam:[now description] key:@"lastupdate"];
		
	} else
	  {
		  _performUpdate = NO;
	  }
	
	
	
	self = [ super initWithFrame: rect ];
	_rect = rect;
	//NSLog(@"MainView: initWithFrame");
	
	UITable *_mainViewBackground = [[UITable alloc] initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y, _rect.size.width,
																			  _rect.size.height )];
	[self addSubview:_mainViewBackground];
	[_mainViewBackground release];
	
	_transitionView =  [ [ UITransitionView alloc ] 
						initWithFrame:
						CGRectMake(_rect.origin.x, _rect.origin.y, _rect.size.width,
								   _rect.size.height)
						];
	
	_browseView = [ [ BrowseView alloc ] initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y
																   , _rect.size.width,
																   _rect.size.height - 49.0f) ];
	[_browseView setDelegate:self];
	_searchView = [ [ SearchCountryView alloc ] initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y
																		  , _rect.size.width,
																		  _rect.size.height) ];
	
	[_searchView setDelegate:self];
	
	
	_favView = [ [ FavouritesView alloc ] initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y
																	, _rect.size.width,
																	_rect.size.height - 49.0f) ];
	[_favView setDelegate:self];
	
	_prefView = [ PreferencesView alloc ]; 
	
	[_prefView setDataManager:_dm];
	[_prefView setDelegate:self];
	
	[_prefView  initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y
										  , _rect.size.width,
										  _rect.size.height) ];
	
	_aboutView = [ AboutView alloc ]; 
	
	[_aboutView setDelegate:self];
	
	[_aboutView  initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y
										   , _rect.size.width,
										   _rect.size.height) ];	
	
	[ self addSubview: _transitionView];
	
	[_transitionView transition:3  toView:_browseView];
	_activeView = _browseView;
	
	_buttonBar = [ [ UIButtonBar alloc ] 
				  initInView: self
				  withFrame: CGRectMake(0.0f, 411.0f, 320.0f, 49.0f)
				  withItemList: [ self buttonBarItems ] ];
    [_buttonBar setDelegate:self];
	
	
    [_buttonBar setBarStyle:1];
    [_buttonBar setButtonBarTrackingMode: 2];
	
    int buttons[5] = { 1, 2, 3, 4, 5 };
    [_buttonBar registerButtonGroup:0 withButtons:buttons withCount: 5];
    [_buttonBar showButtonGroup: 0 withDuration: 0.0f];
    int tag;
	
    for(tag = 1; tag < 5; tag++) {
        [ [ _buttonBar viewWithTag:tag ] 
		 setFrame:CGRectMake(2.0f + ((tag - 1) * 64.0f), 1.0f,64.0f, 48.0f)
		 ];
    }
    [ _buttonBar showSelectionForButton: 1];
	if ([_prefView getnewAvailableCountries]>0)	[_buttonBar setBadgeValue:[ NSString stringWithFormat: @"%d", [_prefView getnewAvailableCountries] ] forButton:4];
	
	currentView = CUR_BROWSER;
	
	
	//[ self addSubview: _buttonBar ];
	
	//[self setPrefBadge:1];
	
	if ( [[_dm getInstalledCountries] count]<1) {
		UIAlertSheet *_nodatasheet = [ [ UIAlertSheet alloc ] initWithFrame:
									  CGRectMake(0, 240, 320, 240) ];
		[ _nodatasheet setTitle:@"No database installed" ];
		[ _nodatasheet setBodyText:@"You have currently no database installed.\nPlease select any dataset you want to have locally available by adding it with the green 'add' buttons in the Preferences section!" ];
		[ _nodatasheet addButtonWithTitle:@"OK" ];
		[ _nodatasheet setDelegate: self ];
		[_nodatasheet popupAlertAnimated:YES];
	}
	
	return self;
	
}

- (void) dealloc {
	
	[ _buttonBar release];
	[_dm release];
	
	
	[super dealloc];
}

- (BOOL) getPerformUpdate {
	return _performUpdate;
}

- (NSArray *)buttonBarItems {
    return [ NSArray arrayWithObjects:
			[ NSDictionary dictionaryWithObjectsAndKeys:
			 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
			 @"Browse.png", kUIButtonBarButtonInfo,
			 @"BrowseSelected.png", kUIButtonBarButtonSelectedInfo,
			 [ NSNumber numberWithInt: 1], kUIButtonBarButtonTag,
			 self, kUIButtonBarButtonTarget,
			 @"Browse", kUIButtonBarButtonTitle,
			 @"0", kUIButtonBarButtonType,
			 nil 
			 ],
			
			[ NSDictionary dictionaryWithObjectsAndKeys:
			 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
			 @"Search.png", kUIButtonBarButtonInfo,
			 @"SearchSelected.png", kUIButtonBarButtonSelectedInfo,
			 [ NSNumber numberWithInt: 2], kUIButtonBarButtonTag,
			 self, kUIButtonBarButtonTarget,
			 @"Search", kUIButtonBarButtonTitle,
			 @"0", kUIButtonBarButtonType,
			 nil 
			 ], 
			
			[ NSDictionary dictionaryWithObjectsAndKeys:
			 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
			 @"Bookmarks.png", kUIButtonBarButtonInfo,
			 @"BookmarksSelected.png", kUIButtonBarButtonSelectedInfo,
			 [ NSNumber numberWithInt: 3], kUIButtonBarButtonTag,
			 self, kUIButtonBarButtonTarget,
			 @"Favourites", kUIButtonBarButtonTitle,
			 @"0", kUIButtonBarButtonType,
			 nil
			 ],
			
			[ NSDictionary dictionaryWithObjectsAndKeys:
			 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
			 @"Preferences.png", kUIButtonBarButtonInfo,
			 @"PreferencesSelected.png", kUIButtonBarButtonSelectedInfo,
			 [ NSNumber numberWithInt: 4], kUIButtonBarButtonTag,
			 self, kUIButtonBarButtonTarget,
			 @"Preferences", kUIButtonBarButtonTitle,
			 @"0", kUIButtonBarButtonType,
			 nil
			 ], 
			
			[ NSDictionary dictionaryWithObjectsAndKeys:
			 @"buttonBarItemTapped:", kUIButtonBarButtonAction,
			 @"About.png", kUIButtonBarButtonInfo,
			 @"AboutSelected.png", kUIButtonBarButtonSelectedInfo,
			 [ NSNumber numberWithInt: 5], kUIButtonBarButtonTag,
			 self, kUIButtonBarButtonTarget,
			 @"About", kUIButtonBarButtonTitle,
			 @"0", kUIButtonBarButtonType,
			 nil
			 ], 
			
			nil
			];
}

- (void) reloadBrowseAndSearch {
	
	[_browseView release];
	_browseView = [ [ BrowseView alloc ] initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y
																   , _rect.size.width,
																   _rect.size.height - 49.0f) ];
	[_browseView setDelegate:self];
	[_searchView release];
	_searchView = [ [ SearchCountryView alloc ] initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y
																		  , _rect.size.width,
																		  _rect.size.height) ];
	
	[_searchView setDelegate:self];
	
	[_prefView release];
	_prefView = [ PreferencesView alloc ]; 
	
	[_prefView setDataManager:_dm];
	[_prefView setDelegate:self];
	
	[_prefView  initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y
										  , _rect.size.width,
										  _rect.size.height) ];
	
	
}

- (void)buttonBarItemTapped:(id) sender {
    int button = [ sender tag ];
	LKAnimation *myAnimObj = [LKTransition animation];
	
	if (currentView == CUR_PREF) {
		
		if ([_prefView getHasChanged]) {
			
			[_prefView processChanges];
			[self reloadBrowseAndSearch];
			
			
		}
		
	}
	
	
    switch (button) {
        case 1:
			if (currentView == CUR_BROWSER) break;
			currentView = CUR_BROWSER;
			
			[myAnimObj setType: @"zoomyOut"];
			[myAnimObj setSubtype:@"fromLeft"];
			[myAnimObj setTimingFunction:[LKTimingFunction functionWithName:@"easeInEaseOut"]];
			[myAnimObj setTransitionFlags: 3];
			[myAnimObj setFillMode:@"extended"];
			[myAnimObj setSpeed: 0.55];
			
			[[_transitionView _layer] addAnimation: myAnimObj forKey: 0];
			
			[_transitionView transition:0 fromView:_activeView toView:_browseView];
			
			_activeView = _browseView;
            break;
			case 2:
			if (currentView == CUR_SEARCH) break;
			currentView = CUR_SEARCH;
			
			
			//			LKAnimation *myAnimObj = [LKTransition animation];
			[myAnimObj setType: @"zoomyOut"];
			[myAnimObj setSubtype:@"fromLeft"];
			[myAnimObj setTimingFunction:[LKTimingFunction functionWithName:@"easeInEaseOut"]];
			[myAnimObj setTransitionFlags: 3];
			[myAnimObj setFillMode:@"extended"];
			[myAnimObj setSpeed:0.55];
			
			[[_transitionView _layer] addAnimation: myAnimObj forKey: 0];
			
			[_transitionView transition:0 fromView:_activeView toView:_searchView];
			_activeView = _searchView;
			
			
			
			//[_buttonBar removeFromSuperview];
			break;
			case 3:
			if (currentView == CUR_FAV) break;
			currentView = CUR_FAV;
			
			[_favView release];
			_favView = [ [ FavouritesView alloc ] initWithFrame: CGRectMake(_rect.origin.x, _rect.origin.y
																			, _rect.size.width,
																			_rect.size.height - 49.0f) ];
			[_favView setDelegate:self];
			
			//			LKAnimation *myAnimObj = [LKTransition animation];
			[myAnimObj setType: @"zoomyOut"];
			[myAnimObj setSubtype:@"fromLeft"];
			[myAnimObj setTimingFunction:[LKTimingFunction functionWithName:@"easeInEaseOut"]];
			[myAnimObj setTransitionFlags: 3];
			[myAnimObj setFillMode:@"extended"];
			[myAnimObj setSpeed:0.55];
			
			[[_transitionView _layer] addAnimation: myAnimObj forKey: 0];
			
			[_transitionView transition:0 fromView:_activeView toView:_favView];
			_activeView = _favView;
			break;
			case 4:
			if (currentView == CUR_PREF) break;
			currentView = CUR_PREF;
			
			//			LKAnimation *myAnimObj = [LKTransition animation];
			[myAnimObj setType: @"zoomyOut"];
			[myAnimObj setSubtype:@"fromLeft"];
			[myAnimObj setTimingFunction:[LKTimingFunction functionWithName:@"easeInEaseOut"]];
			[myAnimObj setTransitionFlags: 3];
			[myAnimObj setFillMode:@"extended"];
			[myAnimObj setSpeed:0.55];
			
			[[_transitionView _layer] addAnimation: myAnimObj forKey: 0];
			[_buttonBar setBadgeValue:nil forButton:4];
			
			[_transitionView transition:0 fromView:_activeView toView:_prefView];
			_activeView = _prefView;
			break;
			case 5:
			
			if (currentView == CUR_ABOUT) break;
			currentView = CUR_ABOUT;
			
			//			LKAnimation *myAnimObj = [LKTransition animation];
			[myAnimObj setType: @"zoomyOut"];
			[myAnimObj setSubtype:@"fromLeft"];
			[myAnimObj setTimingFunction:[LKTimingFunction functionWithName:@"easeInEaseOut"]];
			[myAnimObj setTransitionFlags: 3];
			[myAnimObj setFillMode:@"extended"];
			[myAnimObj setSpeed:0.55];
			
			[[_transitionView _layer] addAnimation: myAnimObj forKey: 0];
			
			[_transitionView transition:0 fromView:_activeView toView:_aboutView];
			_activeView = _aboutView;
			break;
    }
	
}

-(UIButtonBar*)getButtonBar {
	return _buttonBar;
}

- (void)alertSheet:(UIAlertSheet *)sheet buttonClicked:(int)button {
	
	[sheet dismiss];
	[ _buttonBar showSelectionForButton: 4];
	currentView = CUR_PREF;
	
	LKAnimation *myAnimObj = [LKTransition animation];
	[myAnimObj setType: @"zoomyOut"];
	[myAnimObj setSubtype:@"fromLeft"];
	[myAnimObj setTimingFunction:[LKTimingFunction functionWithName:@"easeInEaseOut"]];
	[myAnimObj setTransitionFlags: 3];
	[myAnimObj setFillMode:@"extended"];
	[myAnimObj setSpeed:0.55];
	
	[[_transitionView _layer] addAnimation: myAnimObj forKey: 0];
	[_buttonBar setBadgeValue:nil forButton:4];
	
	[_transitionView transition:0 toView:_prefView];
	_activeView = _prefView;
	
	
}


@end
